package com.aliwo.controller;

import com.aliwo.entity.Student;
import com.aliwo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xuyayuan
 * @data 2021年06月04日
 */
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/insertStudent", method = RequestMethod.POST)
    @ResponseBody
    public Integer insetStudent(Student student) {
        return studentService.insertStudent(student);
    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ResponseBody
    public List<Student> findStudentsBelowAge(Integer age) {
        return studentService.findStudentsBelowAge(age);
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public Integer countStudent() {
        return studentService.countStudent();
    }

    @RequestMapping(value = "/listtoMap", method = RequestMethod.GET)
    @ResponseBody
    public Object listtoMap() {
        List<Student> list = new ArrayList<>();

        Student student = new Student();
        student.setId(100);
        student.setAge(12);
        student.setRealname("xuyy");

        Student student2 = new Student();
        student2.setId(101);
        student2.setAge(13);
        student2.setRealname("xuyy2");

        Student student4 = new Student();
        student4.setId(101);
        student4.setAge(134);
        student4.setRealname("xuyy24");

        Student student3 = new Student();
        student3.setId(103);
        student3.setAge(14);
        student3.setRealname("xuyy3");

        list.add(student);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        Map<Integer, Student> map = list.stream().collect(Collectors.toMap(Student::getId, Function.identity(),
                (key1, key2) -> key2));
        System.out.println(map.size());
        System.out.println(map);
        return map;
    }
}
